<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-if="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="租赁人" prop="leaseholder">
              <el-input v-model="queryParams.leaseholder" placeholder="请输入租赁人" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="联系方式" prop="contactInformation">
              <el-input v-model="queryParams.contactInformation" placeholder="请输入联系方式" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="楼&nbsp;&nbsp;栋&nbsp;&nbsp;号" prop="towerNumber">
              <el-input v-model="queryParams.towerNumber" placeholder="请输入楼栋号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="房&nbsp;&nbsp;间&nbsp;&nbsp;号" prop="roomNumber">
              <el-input v-model="queryParams.roomNumber" placeholder="请输入房间号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="退&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;租" prop="rentOutType">
              <el-select v-model="queryParams.rentOutType" placeholder="请选择退租状态">
                <el-option v-for="dict in park_surrender_tenancy_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col style="text-align: right" :span="24">
            <el-button type="warning" icon="Download" @click="handleExport">导出</el-button>
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()">删除</el-button>
          </el-col>
          <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar> -->
        </el-row>
      </template>

      <el-table v-loading="loading" :data="parkHistoricalLeaseList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="主键" align="center" prop="id" />
        <el-table-column v-if="false" label="园区基础信息主键" align="center" prop="parkFoundationInfoId" />
        <el-table-column v-if="false" label="园区租户主键" align="center" prop="parkTenantId" />
        <el-table-column label="序号 " align="center">
          <template #default="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="租赁人" align="center" prop="leaseholder" />
        <el-table-column label="联系方式" align="center" prop="contactInformation" />
        <el-table-column label="开始时间" align="center" prop="startTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="endTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="退租时间" align="center" prop="rentOutTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.rentOutTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <el-table-column label="楼栋号" align="center" prop="towerNumber" />
        <el-table-column label="所在楼层" align="center" prop="floor" />
        <el-table-column label="房间号" align="center" prop="roomNumber" />
        <el-table-column label="建筑面积(㎡)" align="center" prop="floorArea" />
        <el-table-column label="退租类型" align="center" prop="rentOutType">
          <template #default="scope">
            <dict-tag :options="park_surrender_tenancy_status" :value="scope.row.rentOutType" />
          </template>
        </el-table-column>
        <el-table-column width="120" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleOpenDialog('detail', scope.row.parkBuildingId, scope.row.parkTenantId)"> 详情 </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改园区历史租赁对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="parkHistoricalLeaseFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="园区基础信息主键" prop="parkFoundationInfoId">
          <el-input v-model="form.parkFoundationInfoId" placeholder="请输入园区基础信息主键" />
        </el-form-item>
        <el-form-item label="园区租户主键" prop="parkTenantId">
          <el-input v-model="form.parkTenantId" placeholder="请输入园区租户主键" />
        </el-form-item>
        <el-form-item label="租赁人" prop="leaseholder">
          <el-input v-model="form.leaseholder" placeholder="请输入租赁人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactInformation">
          <el-input v-model="form.contactInformation" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker v-model="form.startTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择开始时间"> </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker v-model="form.endTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择结束时间"> </el-date-picker>
        </el-form-item>
        <el-form-item label="退租时间" prop="rentOutTime">
          <el-date-picker v-model="form.rentOutTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择退租时间"> </el-date-picker>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="楼&nbsp;&nbsp;栋&nbsp;&nbsp;号" prop="towerNumber">
          <el-input v-model="form.towerNumber" placeholder="请输入楼栋号" />
        </el-form-item>
        <el-form-item label="所在楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入所在楼层" />
        </el-form-item>
        <el-form-item label="房&nbsp;&nbsp;间&nbsp;&nbsp;号" prop="roomNumber">
          <el-input v-model="form.roomNumber" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="建筑面积(㎡)" prop="floorArea">
          <el-input v-model="form.floorArea" placeholder="请输入建筑面积" />
        </el-form-item>
        <el-form-item label="退租类型;退租类型(在租:under_rent,提前退租:early_renunciation,到期退租:return lease_at_maturity)" prop="rentOutType">
          <el-select
            v-model="form.rentOutType"
            placeholder="请选择退租类型;退租类型(在租:under_rent,提前退租:early_renunciation,到期退租:return lease_at_maturity)"
          >
            <el-option v-for="dict in park_surrender_tenancy_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="creatTime">
          <el-date-picker v-model="form.creatTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择创建时间"> </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <formDialog ref="formDialogRef" />
  </div>
</template>

<script setup name="ParkHistoricalLease" lang="ts">
import formDialog from './form.vue';
import {
  listParkHistoricalLease,
  getParkHistoricalLease,
  delParkHistoricalLease,
  addParkHistoricalLease,
  updateParkHistoricalLease
} from '@/api/zichanguanli/parkHistoricalLease/index';
import { ParkHistoricalLeaseVO, ParkHistoricalLeaseQuery, ParkHistoricalLeaseForm } from '@/api/zichanguanli/parkHistoricalLease/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { park_surrender_tenancy_status } = toRefs<any>(proxy?.useDict('park_surrender_tenancy_status'));

const parkHistoricalLeaseList = ref<ParkHistoricalLeaseVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const parkHistoricalLeaseFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ParkHistoricalLeaseForm = {
  id: undefined,
  parkFoundationInfoId: undefined,
  parkTenantId: undefined,
  leaseholder: undefined,
  contactInformation: undefined,
  startTime: undefined,
  endTime: undefined,
  rentOutTime: undefined,
  projectName: undefined,
  towerNumber: undefined,
  floor: undefined,
  roomNumber: undefined,
  floorArea: undefined,
  rentOutType: undefined,
  creatTime: undefined
};
const data = reactive<PageData<ParkHistoricalLeaseForm, ParkHistoricalLeaseQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    leaseholder: undefined,
    contactInformation: undefined,
    projectName: undefined,
    rentOutType: undefined,
    towerNumber: undefined,
    roomNumber: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键不能为空', trigger: 'blur' }],
    parkFoundationInfoId: [{ required: true, message: '园区基础信息主键不能为空', trigger: 'blur' }],
    parkTenantId: [{ required: true, message: '园区租户主键不能为空', trigger: 'blur' }],
    leaseholder: [{ required: true, message: '租赁人不能为空', trigger: 'blur' }],
    contactInformation: [{ required: true, message: '联系方式不能为空', trigger: 'blur' }],
    projectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }],
    towerNumber: [{ required: true, message: '楼栋号不能为空', trigger: 'blur' }],
    floor: [{ required: true, message: '所在楼层不能为空', trigger: 'blur' }],
    roomNumber: [{ required: true, message: '房间号不能为空', trigger: 'blur' }],
    floorArea: [{ required: true, message: '建筑面积不能为空', trigger: 'blur' }],
    rentOutType: [
      {
        required: true,
        message: '退租类型;退租类型(在租:under_rent,提前退租:early_renunciation,到期退租:return lease_at_maturity)不能为空',
        trigger: 'change'
      }
    ],
    creatTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);
const formDialogRef = ref();
const handleOpenDialog = (type: string, parkBuildingId?: string | number, parkTenantId?: string | number) => {
  formDialogRef.value.open(type, parkBuildingId, parkTenantId);
};

/** 查询园区历史租赁列表 */
const getList = async () => {
  loading.value = true;
  const res = await listParkHistoricalLease(queryParams.value);
  parkHistoricalLeaseList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  parkHistoricalLeaseFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: ParkHistoricalLeaseVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加园区历史租赁';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: ParkHistoricalLeaseVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getParkHistoricalLease(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改园区历史租赁';
};

/** 提交按钮 */
const submitForm = () => {
  parkHistoricalLeaseFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateParkHistoricalLease(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addParkHistoricalLease(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: ParkHistoricalLeaseVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('确定删除所选的信息吗？').finally(() => (loading.value = false));
  await delParkHistoricalLease(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'zichanguanli/parkHistoricalLease/export',
    {
      ...queryParams.value
    },
    `parkHistoricalLease_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
<style scoped>
/* el-table 列数据为空自动显示 -- */
::v-deep .el-table .el-table__body td .cell:empty::after,
.el-table .el-table__body td .cell > span:empty::after,
.el-table .el-table__body td .cell .approval:empty::after,
.el-table .el-table__body td .cell div:empty::after,
.el-table .el-table__body td .cell .line-limit-length:empty::after {
  content: '-';
}
</style>
